class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
        
        
class Solution(object):
    def isSameTree(self, p:TreeNode, q:TreeNode)->bool:
        """
        :type p: TreeNode
        :type q: TreeNode
        :rtype: bool
        """
        if p ==None and q==None:
            return True
        elif p!=None and q!=None:
            if p.val!=q.val:
                return False
            else:
                return self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)
        else:
            return False

    